﻿using JY.Owin.Security.YahooJapan;
using System;

namespace Owin
{
    public static class YahooJpnAuthenticationExtensions
    {
        /// <summary>
        /// Authenticate users using Yahoo
        /// </summary>
        /// <param name="app">The <see cref="IAppBuilder"/> passed to the configuration method</param>
        /// <param name="options">Middleware configuration options</param>
        /// <returns>The updated <see cref="IAppBuilder"/></returns>
        public static IAppBuilder UseYahooAuthentication(this IAppBuilder app, YahooJpnOAuth2AuthenticationOptions options)
        {
            if (app == null)
            {
                throw new ArgumentNullException("app");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }

            app.Use(typeof(YahooJpnOAuth2AuthenticationMiddleware), app, options);
            return app;
        }

        /// <summary>
        /// Authenticate users using Yahoo
        /// </summary>
        /// <param name="app">The <see cref="IAppBuilder"/> passed to the configuration method</param>
        /// <returns>The updated <see cref="IAppBuilder"/></returns>
        public static IAppBuilder UseYahooAuthentication(
            this IAppBuilder app)
        {
            return UseYahooAuthentication(
                app,
                new YahooJpnOAuth2AuthenticationOptions());
        }

        /// <summary>
        /// Authenticate users using Yahoo Japan OAuth 2.0
        /// </summary>
        /// <param name="app">The <see cref="IAppBuilder"/> passed to the configuration method</param>
        /// <param name="clientId">The Yahoo assigned app id</param>
        /// <param name="clientSecret">The Yahoo assigned app secret</param>
        /// <returns>The updated <see cref="IAppBuilder"/></returns>
        public static IAppBuilder UseYahooAuthentication(
            this IAppBuilder app,
            string appId,
            string appSecret)
        {
            return UseYahooAuthentication(
                app,
                new YahooJpnOAuth2AuthenticationOptions
                {
                    ClientId = appId,
                    ClientSecret = appSecret
                });
        }
    }
}
